package com.iflytek.autofly.setting.biz.sound;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

/**
 * <b>Description: </b>十字线<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015年6月11日</td>
 * <td>上午10:35:48</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
class BalancePanel extends FrameLayout {

    /* 逻辑位置 */
    private float mX;
    private float mY;
    /**
     * 中心点图片的resid
     */
    private int mDotResId = -1;
    /**
     * x轴图片的resid
     */
    private int mXResId = -1;
    /**
     * y轴图片的resid
     */
    private int mYResId = -1;
    /* 作图相关 */
    private Paint mPaint = new Paint();
    private Bitmap mXBitmap = null;
    private Bitmap mYBitmap = null;
    private Bitmap mDotBitmap = null;
    private int mRangeResId;
    private Bitmap mRangeBitmap;

    private int mMinX;
    private int mMaxX;
    private int mMinY;
    private int mMaxY;
    private float mStepX;
    private float mStepY;
    private OnTouchListener mListener;
    private float mStartX;
    private float mStartY;

    public BalancePanel(Context context) {
        super(context);
    }

    public BalancePanel(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BalancePanel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setRangeId(int resId) {
        mRangeResId = resId;
    }

    public void setCrossDotId(int resId) {
        mDotResId = resId;
    }

    public void setXId(int resId) {
        mXResId = resId;
    }

    public void setYId(int resId) {
        mYResId = resId;
    }

    public void setRange(int min, int max) {
        mMinX = min;
        mMaxX = max;
        mMinY = min;
        mMaxY = max;
    }

    public void setOnTouchListener(OnTouchListener l) {
        mListener = l;
    }

    /**
     * moveTo:移动到坐标(x,y). <br/>
     *
     * @param x x坐标
     * @param y y坐标
     */
    public void moveTo(float x, float y) {
        mX = x;
        mY = y;
        invalidate();
    }

    /**
     * moveToX:移动到x，y保持不变. <br/>
     *
     * @param x x坐标
     */
    public void moveToX(float x) {
        mX = x;
        invalidate();
    }

    /**
     * moveToY:移动到y，x保持不变. <br/>
     *
     * @param y y坐标
     */
    public void moveToY(float y) {
        mY = y;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mXResId == -1 || mYResId == -1 || mDotResId == -1 || mMinX >= mMaxX || mMinY >= mMaxY) {
            return;
        }

        // 画范围背景
        if (mRangeBitmap == null) {
            mRangeBitmap = BitmapFactory.decodeResource(this.getResources(),
                    mRangeResId);
            mStepX = ((float) mRangeBitmap.getWidth()) / (mMaxX - mMinX);
            mStepY = ((float) mRangeBitmap.getHeight()) / (mMaxY - mMinY);
            mStartX = ((float) getWidth() - mRangeBitmap.getWidth()) / 2;
            mStartY = ((float) getHeight() - mRangeBitmap.getHeight()) / 2;
        }
        float x = (mX - mMinX) * mStepX + mStartX;
        float y = (mY - mMinY) * mStepY + mStartY;

        canvas.drawBitmap(mRangeBitmap,
                mStartX,
                mStartY,
                mPaint);

        // 画x轴
        if (mXBitmap == null) {
            mXBitmap = BitmapFactory.decodeResource(this.getResources(),
                    mXResId);
        }
        canvas.drawBitmap(mXBitmap,
                ((float) getWidth() - mXBitmap.getWidth()) / 2,
                y - ((float) mXBitmap.getHeight()) / 2,
                mPaint);

        // 画y轴
        if (mYBitmap == null) {
            mYBitmap = BitmapFactory.decodeResource(this.getResources(),
                    mYResId);
        }
        canvas.drawBitmap(mYBitmap,
                x - ((float) mYBitmap.getWidth()) / 2,
                ((float) getHeight() - mYBitmap.getHeight()) / 2,
                mPaint);

        // 画中心点
        if (mDotBitmap == null) {
            mDotBitmap = BitmapFactory.decodeResource(this.getResources(),
                    mDotResId);
        }
        canvas.drawBitmap(mDotBitmap,
                x - ((float) mDotBitmap.getWidth()) / 2,
                y - ((float) mDotBitmap.getHeight()) / 2,
                mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();
        int logicX = (int) ((x - mStartX + 0.5f) / mStepX + mMinX);
        int logicY = (int) ((y - mStartY + 0.5f) / mStepY + mMinX);
        if (logicX < mMinX) {
            logicX = mMinX;
        }
        if (logicX > mMaxX) {
            logicX = mMaxX;
        }
        if (logicY < mMinY) {
            logicY = mMinY;
        }
        if (logicY > mMaxY) {
            logicY = mMaxY;
        }
        moveTo(logicX, logicY);
        if (mListener != null) {
            mListener.onTouch(logicX, logicY, event.getAction());
            return true;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        destroyBitMaps();
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == View.GONE) {
            destroyBitMaps();
        }
    }

    private void destroyBitMaps() {
        if (mRangeBitmap != null) {
            mRangeBitmap.recycle();
            mRangeBitmap = null;
        }
        if (mXBitmap != null) {
            mXBitmap.recycle();
            mXBitmap = null;
        }
        if (mYBitmap != null) {
            mYBitmap.recycle();
            mYBitmap = null;
        }
        if (mDotBitmap != null) {
            mDotBitmap.recycle();
            mDotBitmap = null;
        }
    }

    public interface OnTouchListener {
        boolean onTouch(int x, int y, int action);
    }

}